﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ProDeletorGUI
{
    class TaskList
    {
        public Task task;
        public List<Task> taskList;
        int modeValue;

        public TaskList()
        {
            taskList = new List<Task>();
        }

        public void readControlFile(string controlFile)
        {
            string path;
            XmlTextReader textReader = new XmlTextReader(controlFile);
            //taskList = new List<Task>();
            string attribute;

            while (textReader.Read())
            {
                if (textReader.IsStartElement())
                {
                    switch (textReader.Name.ToLower())
                    {
                        case "folder":
                            attribute = textReader["path"];
                            if (attribute != null)
                            {
                                path = attribute;
                                task = new Task();
                                task.Folder = path;
                                taskList.Add(task);
                            }
                            break;

                        case "logpath":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                task.LogPath = attribute;
                            }
                            break;
                        case "filemask":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                task.FileMask = attribute;
                            }
                            break;
                        case "name":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                task.Name = attribute;
                            }
                            break;
                        case "triggertime":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                string[] trigger = attribute.Split(':');
                                task.Hour = int.Parse(trigger[0]);
                                task.Minute = int.Parse(trigger[1]);
                            }
                            break;

                        case "keepdays":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                task.KeepDays = int.Parse(attribute);
                                //keepminutes += int.Parse(attribute) * 1440;
                            }
                            break;

                        case "keephours":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                task.KeepHours = int.Parse(attribute);
                                //keepminutes += int.Parse(attribute) * 60;
                            }
                            break;

                        case "keepminutes":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                task.KeepMinutes = int.Parse(attribute);
                            }
                            break;

                        case "recursion":
                            attribute = textReader["value"].ToLower();
                            if (attribute != null)
                            {
                                if (attribute.CompareTo("true") == 0)
                                    task.Recursive = true;
                                else
                                    task.Recursive = false;
                            }
                            break;

                        case "emptyfolders":
                            attribute = textReader["value"].ToLower();
                            if (attribute != null)
                            {
                                if (attribute.CompareTo("true") == 0)
                                    task.EmptyFolder = true;
                                else
                                    task.EmptyFolder = false;
                            }
                            break;

                        case "uselog":
                            attribute = textReader["value"].ToLower();
                            if (attribute != null)
                            {
                                if (attribute.CompareTo("true") == 0)
                                    task.Logging = true;
                                else
                                    task.Logging = false;
                            }
                            break;

                        case "testmode":
                            attribute = textReader["value"].ToLower();
                            if (attribute != null)
                            {
                                if (attribute.CompareTo("true") == 0)
                                    task.TestMode = true;
                                else
                                    task.TestMode = false;
                            }
                            break;

                        case "archive":
                            attribute = textReader["value"].ToLower();
                            if (attribute != null)
                            {
                                if (attribute.CompareTo("true") == 0)
                                    task.Archive = true;
                                else
                                    task.Archive = false;
                            }

                            break;
                        case "archivefolder":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                task.ArchiveFolder = attribute;
                            }
                            break;
                        case "mode":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                if (attribute.CompareTo("0") == 0)
                                    task.Mode = "Last accessed";
                                if (attribute.CompareTo("1") == 0)
                                    task.Mode = "Last modified";
                                if (attribute.CompareTo("2") == 0)
                                    task.Mode = "Created";
                                //task.Mode = attribute;
                            }
                            break;

                        case "triggermode":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                if (attribute.CompareTo("0") == 0)
                                    task.TriggerMode = "Daily";
                                if (attribute.CompareTo("1") == 0)
                                    task.TriggerMode = "Repeat";
                                //task.TriggerMode = attribute;
                            }
                            break;

                        // weekdays
                        case "weekdays":
                            attribute = textReader["value"];
                            if (attribute != null)
                            {
                                string[] days = attribute.Split(',');
                                foreach (string day in days)
                                {
                                    if (day.CompareTo("1") == 0)
                                        task.Monday = true;

                                    if (day.CompareTo("2") == 0)
                                        task.Tuesday = true;

                                    if (day.CompareTo("3") == 0)
                                        task.Wednesday = true;

                                    if (day.CompareTo("4") == 0)
                                        task.Thursday = true;

                                    if (day.CompareTo("5") == 0)
                                        task.Friday = true;

                                    if (day.CompareTo("6") == 0)
                                        task.Saturday = true;

                                    if (day.CompareTo("7") == 0)
                                        task.Sunday = true;

                                }
                            }
                            break;
                    }
                }
            }
        }

        public void writeControlFile(string controlFile)
        {
            try
            {
                XmlTextWriter textWriter = new XmlTextWriter(controlFile, null);
                textWriter.Formatting = Formatting.Indented;
                textWriter.WriteStartDocument();

                textWriter.WriteComment("ProDeletor service control");
                textWriter.WriteStartElement("servicecontrol");

                foreach (Task task in taskList)
                {
                    //folder start
                    textWriter.WriteStartElement("folder");
                    textWriter.WriteAttributeString("path", task.Folder);

                    //filemask
                    textWriter.WriteStartElement("filemask");
                    textWriter.WriteAttributeString("value", task.FileMask);
                    textWriter.WriteEndElement();

                    //name
                    textWriter.WriteStartElement("name");
                    textWriter.WriteAttributeString("value", task.Name);
                    textWriter.WriteEndElement();

                    //triggertime
                    textWriter.WriteStartElement("triggertime");
                    textWriter.WriteAttributeString("value", task.Hour + ":" + task.Minute);
                    textWriter.WriteEndElement();

                    //weekdays
                    textWriter.WriteStartElement("weekdays");
                    string temp = "";
                    if (task.Monday)
                        temp += "1";
                    if (task.Tuesday)
                        if (temp.Length > 0)
                            temp += ",2";
                        else temp += "2";
                    if (task.Wednesday)
                        if (temp.Length > 0)
                            temp += ",3";
                        else temp += "3";
                    if (task.Thursday)
                        if (temp.Length > 0)
                            temp += ",4";
                        else temp += "4";
                    if (task.Friday)
                        if (temp.Length > 0)
                            temp += ",5";
                        else temp += "5";
                    if (task.Saturday)
                        if (temp.Length > 0)
                            temp += ",6";
                        else temp += "6";
                    if (task.Sunday)
                        if (temp.Length > 0)
                            temp += ",7";
                        else temp += "7";
                    textWriter.WriteAttributeString("value", temp);
                    textWriter.WriteEndElement();

                    //recursion
                    textWriter.WriteStartElement("recursion");
                    textWriter.WriteAttributeString("value", task.Recursive.ToString().ToLower());
                    textWriter.WriteEndElement();

                    //emptyFolders
                    textWriter.WriteStartElement("emptyfolders");
                    textWriter.WriteAttributeString("value", task.EmptyFolder.ToString().ToLower());
                    textWriter.WriteEndElement();

                    //keepdays
                    textWriter.WriteStartElement("keepdays");
                    textWriter.WriteAttributeString("value", task.KeepDays.ToString());
                    textWriter.WriteEndElement();

                    //keephours
                    textWriter.WriteStartElement("keephours");
                    textWriter.WriteAttributeString("value", task.KeepHours.ToString());
                    textWriter.WriteEndElement();

                    //keepminutes
                    textWriter.WriteStartElement("keepminutes");
                    textWriter.WriteAttributeString("value", task.KeepMinutes.ToString());
                    textWriter.WriteEndElement();

                    //uselog
                    textWriter.WriteStartElement("uselog");
                    textWriter.WriteAttributeString("value", task.Logging.ToString().ToLower());
                    textWriter.WriteEndElement();

                    //logpath
                    textWriter.WriteStartElement("logpath");
                    textWriter.WriteAttributeString("value", task.LogPath);
                    textWriter.WriteEndElement();

                    //archive
                    textWriter.WriteStartElement("archive");
                    textWriter.WriteAttributeString("value", task.Archive.ToString().ToLower());
                    textWriter.WriteEndElement();

                    //archivefolder
                    textWriter.WriteStartElement("archivefolder");
                    textWriter.WriteAttributeString("value", task.ArchiveFolder);
                    textWriter.WriteEndElement();

                    //mode
                    if (task.Mode.CompareTo("Last accessed") == 0)
                        modeValue = 0;
                    if (task.Mode.CompareTo("Last modified") == 0)
                        modeValue = 1;
                    if (task.Mode.CompareTo("Created") == 0)
                        modeValue = 2;
                    textWriter.WriteStartElement("mode");
                    textWriter.WriteAttributeString("value", modeValue.ToString());
                    textWriter.WriteEndElement();

                    //triggermode
                    if (task.TriggerMode.CompareTo("Daily") == 0)
                        modeValue = 0;
                    if (task.TriggerMode.CompareTo("Repeat") == 0)
                        modeValue = 1;
                    textWriter.WriteStartElement("triggermode");
                    textWriter.WriteAttributeString("value", modeValue.ToString());
                    textWriter.WriteEndElement();

                    //testmode
                    textWriter.WriteStartElement("testmode");
                    textWriter.WriteAttributeString("value", task.TestMode.ToString().ToLower());
                    textWriter.WriteEndElement();

                    //folder end
                    textWriter.WriteEndElement();
                }
                //write ending and close
                textWriter.WriteEndElement();
                textWriter.WriteEndDocument();
                textWriter.Close();
            }
            catch
            {
                Console.WriteLine("Error in writing file!");
                return;
            }
        }
    }
}
